import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import api from '@/api'

export const useDecorationStore = defineStore('decoration', () => {
  // 状态
  const decorationStyles = ref([])
  const roomTypes = ref([])
  const currentRequest = ref(null)
  const requests = ref([])
  const loading = ref(false)
  const error = ref(null)

  // 计算属性
  const isGenerating = computed(() => {
    return currentRequest.value?.status === 'processing' || currentRequest.value?.status === 'pending'
  })

  // 动作
  const fetchStyles = async () => {
    try {
      const response = await api.get('/decoration/styles')
      decorationStyles.value = response.data.styles
    } catch (err) {
      error.value = err.message
      console.error('获取装修风格失败:', err)
    }
  }

  const fetchRoomTypes = async () => {
    try {
      const response = await api.get('/decoration/room-types')
      roomTypes.value = response.data.room_types
    } catch (err) {
      error.value = err.message
      console.error('获取房间类型失败:', err)
    }
  }

  const uploadFile = async (file) => {
    try {
      loading.value = true
      error.value = null
      
      const formData = new FormData()
      formData.append('file', file)
      
      const response = await api.post('/decoration/upload', formData, {
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      })
      
      return response.data
    } catch (err) {
      error.value = err.response?.data?.error || err.message
      throw err
    } finally {
      loading.value = false
    }
  }

  const generatePlan = async (formData) => {
    try {
      loading.value = true
      error.value = null
      
      // 映射为后端所需的下划线命名
      const payload = {
        decoration_style: formData.decoration_style || formData.decorationStyle,
        room_type: formData.room_type || formData.roomType,
        decoration_plan: formData.decoration_plan || formData.decorationPlan,
        budget_min: formData.budget_min ?? formData.budgetMin,
        budget_max: formData.budget_max ?? formData.budgetMax,
        floor_plan_url: formData.floor_plan_url || formData.floorPlanUrl
      }

      const response = await api.post('/decoration/generate', payload)
      currentRequest.value = { id: response.data.request_id, status: 'pending' }
      
      return response.data
    } catch (err) {
      error.value = err.response?.data?.error || err.message
      throw err
    } finally {
      loading.value = false
    }
  }

  const checkRequestStatus = async (requestId) => {
    try {
      const response = await api.get(`/decoration/status/${requestId}`)
      currentRequest.value = response.data
      return response.data
    } catch (err) {
      error.value = err.response?.data?.error || err.message
      throw err
    }
  }

  const fetchRequests = async (page = 1, perPage = 10) => {
    try {
      loading.value = true
      const response = await api.get('/decoration/requests', {
        params: { page, per_page: perPage }
      })
      requests.value = response.data.requests
      return response.data
    } catch (err) {
      error.value = err.response?.data?.error || err.message
      throw err
    } finally {
      loading.value = false
    }
  }

  const clearError = () => {
    error.value = null
  }

  const resetCurrentRequest = () => {
    currentRequest.value = null
  }

  return {
    // 状态
    decorationStyles,
    roomTypes,
    currentRequest,
    requests,
    loading,
    error,
    
    // 计算属性
    isGenerating,
    
    // 动作
    fetchStyles,
    fetchRoomTypes,
    uploadFile,
    generatePlan,
    checkRequestStatus,
    fetchRequests,
    clearError,
    resetCurrentRequest
  }
})
